import { defineStore } from 'pinia'
import type { LeaderTeacherRelation } from '../types/LeaderTeacherRelation'

/**
 * 领导教师关系存储模块
 * 负责管理领导与教师关系的全局状态
 */
export const useLeaderTeacherRelationStore = defineStore('leaderTeacherRelation', {
    // 持久化配置
    persist: {
        storage: localStorage,
        paths: ['relations', 'currentRelation']
    },
    state: () => ({
        // 所有关系列表
        relations: [] as LeaderTeacherRelation[],
        // 当前选中的关系
        currentRelation: null as LeaderTeacherRelation | null,
        // 加载状态
        loading: false
    }),
    actions: {
        /**
         * 获取所有领导教师关系
         */
        async fetchAllRelations() {
            this.loading = true
            try {
                // this.relations = await relationApi.getAllLeaderTeacherRelations()
            } finally {
                this.loading = false
            }
        },

        /**
         * 建立新的领导教师关系
         * @param leaderId - 领导ID
         * @param teacherId - 教师ID
         */
        async createRelation(leaderId: number, teacherId: number) {
            this.loading = true
            try {
                // const newRelation = await relationApi.createLeaderTeacherRelation(leaderId, teacherId)
                // this.relations.push(newRelation)
            } finally {
                this.loading = false
            }
        },

        /**
         * 删除领导教师关系
         * @param relationId - 关系ID
         */
        async deleteRelation(relationId: number) {
            this.loading = true
            try {
                // await relationApi.deleteLeaderTeacherRelation(relationId)
                // this.relations = this.relations.filter(r => r.relationId !== relationId)
            } finally {
                this.loading = false
            }
        }
    },
    getters: {
        /**
         * 根据领导ID获取关系列表
         * @param leaderId - 领导ID
         */
        relationsByLeaderId: (state) => (leaderId: number) =>
            state.relations.filter(r => r.leaderId === leaderId),

        /**
         * 根据教师ID获取关系列表
         * @param teacherId - 教师ID
         */
        relationsByTeacherId: (state) => (teacherId: number) =>
            state.relations.filter(r => r.teacherId === teacherId)
    }
})
